let flag = false;

function getModule() {
  let modules = {}

  const find = (pId) => {
    for (let i = 0; i < modules.length; i++) {
      if (modules[i].moduleId === pId) {
        return modules[i].moduleRName
      }
    }
    return ''
  }

  let module = $('#moduleName').val().trim()
  axiosRequest1('post', `/module/moduleList`, { moduleRName: module }).then((result) => {
    const { code, message, data } = result.data
    if (code === 200) {
      modules = data
      let s = ''
      data.forEach(e => {
        s += `<tr>
          <td>${e.moduleId}</td>
          <td>${e.moduleRName}</td>
          <td>${e.modulePid === 0 ? '无' : find(e.modulePid)}</td>
          <td>${e.moduleMUrl === '' ? '#' : e.moduleMUrl}</td>
          <td>${e.moduleState === '1' ? '正常' : '模块关闭'}</td>
          <td>
            <a href="javascript:void(0);" 
            onclick="update('${e.moduleId}','${e.moduleRName}','${e.modulePid}','${e.moduleMUrl}','${e.moduleState}')" 
            class="tablelink">修改</a>
            <a href="javascript:void(0);" class="tablelink" onclick="disable(${e.moduleId},${e.moduleState})">${e.moduleState === '1' ? '禁用' : '恢复'}</a>
          </td>
        </tr>`
      });
      $('#tbody').html(s)
      $('.tablelist tbody tr:odd').addClass('odd');
    } else showMessage(message, 2000)
  }).catch((err) => {
    console.log(err);
  });
}

function disable(id, state) {
  if (state === 1) {
    tipOpen('确认禁用此模块吗?')
  }else{
    tipOpen('确认恢复此模块吗?')
  }
  timer = setInterval(() => {
    if (flag) {
      tipClose()
      flag = false;
      axiosRequest1('get', `/module/disabled/${id}/${state}`).then((result) => {
        const { message, code } = result.data
        showMessage(message, 2000)
        if (code === 200) {
          getModule()
        }
      }).catch((err) => {
        console.log(err);
      });
    }
  }, 10)

}

function confirm(){
  flag=true
}

function update(id,rname,pid,url){
  let s=`moduleUpdate.html?id=${id}&rname=${rname}&parent=${pid}&url=${url}`;
  location.href=encodeURI(s)

}